package com.shililu.wx.web;


import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.shililu.wx.utils.Constant;
import com.shililu.wx.utils.Security;
import com.shililu.wx.vo.WxLoginResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.UUID;


@RestController
@Slf4j
public class WxCodeController {

    @Autowired
    private RestTemplate restTemplate;

    // Restful服务对应的url地址
    @Value("${user.userServicePath}")
    private String userServicePath;

    @Value("${wx.login}")
    private String wxLogin;

    @Value("${wx.callback}")
    private String wxCallbackUrl;

    @Value("${wx.token}")
    private String wxToken;

    @Value("${wx.scanNew}")
    private String wxScanNeuUrl;

    @Value("${user.name}")
    private String userName;

    @Value("${user.password}")
    private String password;


    @GetMapping("/user/{id}")
    public String getApi(@PathVariable Long id) {
        return "hello world"+id;
    }


    @GetMapping("/template/{id}")
    public String findById(@PathVariable Long id) {
        String u = this.restTemplate.getForObject(this.userServicePath + id, String.class);
        System.out.println(u);
        return u;
    }

    /**
     * 获取apiKey
     * @return
     */
    @GetMapping("/login")
    public Object WxLogin(){
        MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
        map.add("phone","15884553905");
        map.add("password",password);
        log.info(JSON.toJSONString(map));
        ResponseEntity<String> result=restTemplate.postForEntity(this.wxLogin,map,String.class);
        WxLoginResult result1=JSON.parseObject(result.getBody(),WxLoginResult.class);
        Constant.apiKey=result1.getData().getApiKey();
        log.info(Constant.apiKey);
        return result;
    }


    @GetMapping("/wx/scan")
    public Object WxScan(){
        MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
        map.add("callback_url",wxCallbackUrl);
        //封装header
        HttpHeaders headers = new HttpHeaders();
        String hswebtime=System.currentTimeMillis()+"_"+ UUID.randomUUID();
        headers.set("hswebtime",hswebtime);
        headers.set("apikey",Constant.apiKey);
        headers.set("token", Security.md5(hswebtime+this.wxToken));
        log.info("###################scan-hearder###{}", JSON.toJSONString(headers));
        HttpEntity<Object> requestEntity = new HttpEntity<Object>(map, headers);
        log.info(JSON.toJSONString(map));
        ResponseEntity<String> result=restTemplate.postForEntity(this.wxScanNeuUrl,requestEntity,String.class);
        log.info("###################scan###{}", JSON.toJSONString(result));
        return result;
    }




}
